package com.vlin.controller.login;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.session.Session;
import org.springframework.session.SessionRepository;
import org.springframework.web.bind.annotation.RestController;

import com.vlin.entity.user.User;
import com.vlin.mapper.user.UserMapper;
import com.vlin.service.user.LoginService;

@RestController
public class LoginController implements LoginService {
	
	private static final Logger logger = org.slf4j.LoggerFactory.getLogger(LoginController.class);

	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private HttpSession session;
	
	
	@SuppressWarnings("rawtypes")
	@Autowired 
	private SessionRepository sessionRepository;
	 

	@SuppressWarnings("unchecked")
	public String userLogin(String username, String password, String sessionId) {
		User user = userMapper.getUser(username, password);
		logger.info("SessionId is {}", session.getId());
		if (user == null) {
			logger.info("user is null");
			return "";
		}

		Session session = sessionRepository.findById(sessionId);
		session.setAttribute("User", user);
		session.removeAttribute("Login");
		sessionRepository.save(session);
		return sessionId;

	}

}
